<?php

Class m_operator extends CI_Model {

    function get_all_operator() {
        $this->db->select('*');
        $this->db->from('operator');
        $this->db->order_by("id_operator", "DESC");
        $this->db->join('formasi', 'formasi.id_operator_atasan = operator.id_operator', 'left');
        $this->db->group_by('operator.id_operator');

//        $query = $this->db->get();
//        return $query->result();
        return $this->db->get();
    }

    function insert_operator($data) {
        $this->db->insert('operator', $data);

        return $this->db->insert_id();
    }

    function get_by_id($id_operator) {
        $query = $this->db->get_where('operator', array('id_operator' => $id_operator));
        return $query;
    }

    function update_operator($data, $id_operator) {
        $this->db->where('operator.id_operator', $id_operator);
        return $this->db->update('operator', $data);
    }

    function delete_operator($id_operator) {
        $this->db->where('operator.id_operator', $id_operator);
        return $this->db->delete('operator');
    }

    function delete_formasi($id_formasi) {
        $this->db->where('formasi.id_formasi', $id_formasi);
        return $this->db->delete('formasi');
    }

    function cek_nik($nik) {
        $cek = $this->db->get_where('operator', array('nik' => $nik));
        if ($cek->num_rows() > 0) {
            return false;
        }
        return true;
    }

    function tambah_data_tabel($tabel, $data) {
        $this->db->insert($tabel, $data);
        return $this->db->insert_id();
    }

    function tambah_data_tabel_batch($tabel, $data) {
        return $this->db->insert_batch($tabel, $data);
    }

    function get_data_formasi_atasan($id) {
        $this->db->select('formasi.id_formasi as id_formasi, operator.nama as nama, formasi.id_operator_atasan as id_operator_atasan');
        $this->db->from('formasi');
        $this->db->join('operator', 'operator.id_operator = formasi.id_operator_atasan');
        $this->db->where('formasi.id_operator_bawahan', $id);

        return $this->db->get();
    }

    function get_data_formasi_bawahan($id) {
        $this->db->select('formasi.id_formasi as id_formasi, operator.nama as nama, formasi.id_operator_bawahan as id_operator_bawahan');
        $this->db->from('formasi');
        $this->db->join('operator', 'operator.id_operator = formasi.id_operator_bawahan');
        $this->db->where('formasi.id_operator_atasan', $id);

        return $this->db->get();
    }

    function atasan_get($data) {
        $this->db->select('formasi.id_formasi AS id_formasi , operator.nama AS nama');
        $this->db->from('formasi');
        $this->db->join('operator', 'operator.id_operator = formasi.id_operator_atasan');
        $this->db->where_in('formasi.id_formasi', $data);
        return $this->db->get();
    }

    function bawahan_get($data) {
        $this->db->select('formasi.id_formasi AS id_formasi , operator.nama AS nama');
        $this->db->from('formasi');
        $this->db->join('operator', 'operator.id_operator = formasi.id_operator_atasan');
        $this->db->where_in('formasi.id_formasi', $data);
        return $this->db->get();
    }

}